<?php

namespace app\api\controller;

use app\Request;
use app\api\model\Admin as AdminModel;
use Firebase\JWT\JWT;

class login
{
    public function index(Request $request)
    {
        $data = $request->param();

        $db = new AdminModel();
        $info = $db->where('username',$data['username'])->find();
        if (!$info) {
            return json(['code' => 404, 'msg' => '用户不存在']);
        }
        if ($info['password'] != md5($data['password'])) {
            return json(['code' => 404, 'msg' => '密码错误']);
        }

        //jwt
        $jwt = new JWT();
        $key = 'api123456';
        $now = time();
        $expiry = $now + (12 * 60 * 60); // 2 hours from now
        $payload = [
            'iss' => 'http://124.223.83.80:8082',
            'aud' => 'http://124.223.83.80:8082',
            'iat' => $now,
            'nbf' => $now,
            'exp' => $expiry, // 有效时间
            'aid' => $info['id']
        ];
        $token = $jwt::encode($payload, $key, 'HS256');


        return json(['code'=>200,'msg' =>'登录成功','token'=>$token]);
    }
}